<!DOCTYPE html>
<meta name="viewport" content="width=device-width, user-scalable=no">
<link rel="stylesheet" type="text/css" href="resources/tablet.css">
<script src="resources/perf_test_helper.js"></script>

<container id="container"></container>

<script>
var N = PerfTestHelper.getN(500);
var duration = 1000;

function startAnimation() {
  var target = document.createElement('target');
  container.appendChild(target);
  updateStyle(target);
  target.addEventListener('animationend', function(e) {
    updateStyle(target);
  });
}

var nextId = 0;
var nextAnim = 0;
function updateStyle(element) {
  if (!element.id) {
    element.id = 'e' + nextId++;
  }
  if (element.styleSheet) {
    element.styleSheet.remove();
  }
  var id = element.id;
  var anim = id + 'anim' + nextAnim++;
  var style = document.createElement('style');
  style.textContent = '\
  #' + id + ' {\
    animation: ' + anim + ' 1s;\
  }\n\
  @keyframes ' + anim + ' {\
    0% { opacity: 0; }\
    100% { opacity: 1; }\
  }';
  container.appendChild(style);
  element.styleSheet = style;
}

requestAnimationFrame(function(t) {
  var base = t;
  var i = 0;

  function staggeredStart(t) {
    elapsed = t - base;
    for (; i < N * elapsed / duration; i++) {
      startAnimation();
    }
    if (i < N) {
      requestAnimationFrame(staggeredStart);
    } else {
      PerfTestHelper.signalReady();
    }
  }
  requestAnimationFrame(staggeredStart);
});
</script>
